local XMPQT = XLGetGlobal("XMPQT")
XMPQT.MenuFunc = {} 
XMPQT.Window = {}
--显示菜单用
-- 创建菜单，并根据最后个参数table插入菜单项
-- menuTable 每项菜单格式{id, index, Splitter, Text, Icon, OnInit_Func, OnSelect_Func, SubMenu={SubMenuTemplate, SubMenuTable={}}}
-- Table 属性 MenuTemplate, SplitterTemplate 这俩默认 "menu.context.item" "menu.splitter"
-- XMPQT.Window.CreateMenu(2, 2, "single.menu", "tree.system.menu.context", "context_menu"	)

function XMPQT.Window.CreateMenu( x, y, menunHostWndTemplateID, menuTreeTemplateID, menuObjID, parentHandle, menuTable, userData)
	
	local templateMananger = XLGetObject("Xunlei.UIEngine.TemplateManager")
	local menuTreeTemplate = templateMananger:GetTemplate(menuTreeTemplateID,"ObjectTreeTemplate")

	local menuTreeID = menuTreeTemplateID..".Instance"
	local menuTree = menuTreeTemplate:CreateInstance(menuTreeID)
	
	local menuHostWndTemplate = templateMananger:GetTemplate(menunHostWndTemplateID, "HostWndTemplate")
	local menuHostWndID = menunHostWndTemplateID..".Instance"
	local menuHostWnd = menuHostWndTemplate:CreateInstance(menuHostWndID)
	menuHostWnd:SetUserData(userData)
	menuHostWnd:BindUIObjectTree(menuTree)
	
	local menuContext = menuTree:GetUIObject(menuObjID)
	-- tree.media.menu.context 尝试拼出类似XMP.MediaMenu.Menu来当做table表
	if menuTable == nil then
		local pos = string.find(menuTreeTemplateID, "%.")
		if pos >= 0 then
			local secPos = string.find(menuTreeTemplateID, "%.", pos+1)
			if secPos >= 0 then
				local menuName = string.sub(menuTreeTemplateID, pos+1, secPos-1)
				if menuName and #menuName > 0 then
					menuName = string.upper(string.sub(menuName, 1, 1))..string.sub(menuName, 2).."Menu"
					if type(loadstring("return XMPQT."..menuName)()) ~= "nil" then
						menuTable = loadstring("return XMPQT."..menuName..".Menu")()
						
					end
				end
			end
		end
	end

	if menuTable ~= nil then
		-- 默认的菜单项模板
		local splitterTmpl = "menu.splitter"
		local menuTmpl = "menu.context.item"
		if menuTable.SplitterTemplate then
			splitterTmpl = menuTable.SplitterTemplate
		end
		if menuTable.MenuTemplate then
			menuTmpl = menuTable.MenuTemplate
		end
		
		local templateManager = XLGetObject("Xunlei.UIEngine.TemplateManager")
		local menuTemplate = templateManager:GetTemplate(menuTmpl, "ObjectTemplate")
		local splitterTemplate = templateManager:GetTemplate(splitterTmpl, "ObjectTemplate")
		-- 递归创建菜单
		local function AddMenuItems(menuFather, items)
			if not items then
				return false
			end
			for i = 1, #items do
				local menuItem = items[i]
				local addItem = nil
				if menuItem.Splitter then
					addItem = splitterTemplate:CreateInstance(menuItem.id)
				else
					addItem = menuTemplate:CreateInstance(menuItem.id)
					addItem:SetText(menuItem.Text)
				end
				if menuItem.Icon then
					addItem:SetIconID(menuItem.Icon)
				end
				addItem:SetUserData(i)
				
				-- 响应函数默认为Oninit=XMPQT.MenuFunc[id_Oninit],OnSelect=XMPQT.MenuFunc[id]
				local onInitFunc
				if type (menuItem.OnInit_Func) == "function" then
					addItem:AttachListener("OnInit", false, function (self)
							menuItem.OnInit_Func(self)
						end)
				elseif menuTable.MenuTemplate == nil then
					addItem:AttachListener("OnInit", false, function (self)
						if type(XMPQT.MenuFunc[self:GetID().."_OnInit"]) == "function" then
							XMPQT.MenuFunc[self:GetID().."_OnInit"](self)
						end
					end)
				end
				if type (menuItem.OnSelect_Func) == "function" then
					addItem:AttachListener("OnSelect", false, function (self)
							menuItem.OnSelect_Func(self)
						end)
				elseif menuTable.MenuTemplate == nil then
					addItem:AttachListener("OnSelect", false, function (self)
							if type(XMPQT.MenuFunc[self:GetID()]) == "function" then
								XMPQT.MenuFunc[self:GetID()](self)
							end
						end)
				end
				if menuItem.index == nil or menuItem.index <=0 then
					menuFather:AddItem(addItem)
				else
					menuFather:InsertItem(menuItem.index, addItem)
				end
				-- 显示的时候再一项项添加进来
				local function OnShowSubMenu(self)
					local subMenu = self:GetSubMenu()
					AddMenuItems(subMenu, menuItem.SubMenu.SubMenuTable)
				end
				-- 添加该菜单项的子菜单
				if not menuItem.Splitter then
					if menuItem.SubMenu then
						addItem:AttachListener("OnShowSubMenu", false, OnShowSubMenu)
						-- 菜单模板，在NormalMenu.xml里面定义
						if menuItem.SubMenu.SubMenuTemplate then
							addItem:SetSubMenu(menuItem.SubMenu.SubMenuTemplate)
						else
							addItem:SetSubMenu("submenu.template")
						end
					end
				end
			end
		end
		AddMenuItems(menuContext, menuTable)
	end
	
	menuContext:SetFocus( true )
	if not parentHandle then
		parentHandle =XMPQT.mainwnd --XMP.Helper.playpanelwnd:GetWndHandle()--
	end
	local left, top, width, height = menuContext:GetScreenPos(x, y,parentHandle)
	local screenLeft, screenTop, screenRight, screenBottom
	local bFullScreen = 0 --XMP.XmpPlayer.self:Call("IsFullScreen")
	screenLeft, screenTop, screenRight, screenBottom = XMPQT.mainwnd:GetWindowRect()
	--非全屏下获取系统工作区域
	--if bFullScreen or (0== parentHandle)then
	--	screenLeft, screenTop, screenRight, screenBottom = XMP.XmpPre:Call2("GetScreenRectFromPoint", x, y)
	--else
	--	screenLeft, screenTop, screenRight, screenBottom = XMP.XmpPre:Call2("GetSysWorkArea",parentHandle)
	--end
	XMPQT.LOG("GetScreenPos left=", left, ", top=", top, ", width=", width, ", height=", height)
	XMPQT.LOG("GetScreenPos left=", x, ", y=", y)
	local root = menuTree:GetRootObject()
	local root_left, root_top, root_right, root_bottom = root:GetObjPos()
	root:SetObjPos( root_left+left-screenLeft, root_top+top-screenTop, root_right+left-screenLeft, root_bottom+top-screenTop)
	
	if top < y then
		if left < x then
			menuContext:SetPopStatus(2,2)
		else
			menuContext:SetPopStatus(2,1)
		end
	else
		if left < x then
			menuContext:SetPopStatus(1,2)
		else
			menuContext:SetPopStatus(1,1)
		end
	end
	-- 比全屏小一像素
	menuHostWnd:TrackPopupMenu(parentHandle, screenLeft, screenTop+1, screenRight-screenLeft, screenBottom-screenTop-1)
	
	if menuHostWnd:GetMenuMode() == "manual" then
		local objtreeManager = XLGetObject("Xunlei.UIEngine.TreeManager")	
		objtreeManager:DestroyTree(menuTreeID)
		XLGetObject("Xunlei.UIEngine.HostWndManager"):RemoveHostWnd(menuHostWndID)
	end
end
	